﻿namespace UDKDevKitVSXDebugEngine.Utils
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.Debugger.Interop;
    using UDKDevKitVSXDebugEngine.Definitions;
    using UDKDevKitVSXDebugEngine.Exceptions;

    public class Core
    {
        public static void CheckOk(int hr)
        {
            if (hr != 0)
            {
                throw new ComponentException(hr);
            }
        }

        public static void RequireOk(int hr)
        {
            if (hr != 0)
            {
                throw new InvalidOperationException();
            }
        }

        internal static bool IsFlagSet(uint value, int flagValue)
        {
            return (value & flagValue) != 0;
        }

        public static Guid GetProcessId(IDebugProcess2 process)
        {
            Guid processId;
            RequireOk(process.GetProcessId(out processId));
            return processId;
        }

        public static Guid GetProcessId(IDebugProgram2 program)
        {
            IDebugProcess2 process;
            RequireOk(program.GetProcess(out process));
            return GetProcessId(process);
        }

        public static int UnexpectedException(Exception e)
        {
            Debug.Fail("Unexpected exception during Attach");
            return Constants.RPC_E_SERVERFAULT;
        }
    }
}
